package appx.craft.droid.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import appx.craft.droid.R;
import appx.craft.droid.utils.Const;
import appx.craft.droid.utils.Log;
import appx.craft.droid.utils.Pref;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class CurrentLocation extends MapActivity{
	
	public final String TAG = CurrentLocation.class.getCanonicalName();
	private MapView mapView;
	private MapController mController;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.debug(TAG,"[ onCreate ] ");
		setContentView(R.layout.activity_current_location);
		mapView = (MapView)findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		mController = mapView.getController();
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.map_pin_holed_violet_normal);
		AppItemizedOverlay itemizedoverlay = new AppItemizedOverlay(drawable, this);
		
		GeoPoint mGeoPoint = new GeoPoint((int)(Float.valueOf(Pref.getValue(Const.CURRENT_LATITUDE,"0")) * 1E6),(int)(Float.valueOf(Pref.getValue(Const.CURRENT_LONGITUDE,"0"))* 1E6));
		OverlayItem overlayitem = new OverlayItem(mGeoPoint, "Hola, Mundo!", "I'm in Mexico City!");
		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);
		mController.animateTo(mGeoPoint);
		mController.setZoom(50);
		mapView.invalidate();
	}
	@Override
	protected void onStart() {
		super.onStart();
		Log.debug(TAG,"[ onStart ] ");
	}
	@Override
	protected void onStop() {
		super.onStop();
		Log.debug(TAG,"[ onStop ] ");
	}
	@Override
	protected void onPause() {
		super.onPause();
		Log.debug(TAG,"[ onPause ] ");
	}
	@Override
	protected void onResume() {
		super.onResume();
		Log.debug(TAG,"[ onResume ] ");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.debug(TAG,"[ onDestroy ] ");
	}
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private class AppItemizedOverlay extends ItemizedOverlay<OverlayItem>{
		
		private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
		private Context mContext;
		public AppItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
		
		}
		public AppItemizedOverlay(Drawable defaultMarker,Context mContext) {
			super(boundCenterBottom(defaultMarker));
			this.mContext = mContext; 
		}
		
		@Override
		protected OverlayItem createItem(int position) {
			return mOverlays.get(position);
		}

		@Override
		public int size() {
			 return mOverlays.size();
		}
		
		public void addOverlay(OverlayItem overlay) {
		    mOverlays.add(overlay);
		    populate();
		}
	}
}
